package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import controller.UI;

/**
 * Frame is responsible for window with all filter options.
 */
public class LoggerFilterFrame extends JFrame
{ 
	/* Current shown window */
	public static LoggerFilterFrame currentFilterFrame = null;
	
	/* Filter options names */
	private final String[] radioButtonsNames = {"IP", "In", "Accepted", "Port", "Out", "Denied", "Protocol"};
	
	/* Filter options taht need text box */
	private final String[] radioButtonsWithText = {"IP", "Port", "Protocol"};
	
	/* Radio buttons */
	private JRadioButton[] radioButtons = new JRadioButton[radioButtonsNames.length];
	ButtonGroup group = null;
	
	/* Filter parameter */
	private static JTextField textFiled = null;
	
	/* Text */
	private JPanel textPanel = new JPanel();
	
	/**
	 * C'tor
	 * Define the frame.
	 */
	public LoggerFilterFrame()
	{
		/* Window's characteristics */
		setTitle("Filter Options");
	    setSize(280, 180);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());

		/* Radio buttons */
	    addRadioButtons();
	    
	    /* Text panel */
		textPanel.setLayout(new FlowLayout());
		addTextBox();
	    add(textPanel, BorderLayout.CENTER);
		textPanel.setVisible(false);
		
		/* Standart buttons */
	    addStandartButtons();
	    
	    /* Store the current open window */
	    currentFilterFrame = this;
	  }
	
	/**
	 * This method adds radio buttons and their listeners for filtering options
	 */
	private void addRadioButtons() 
	{
		boolean needTextBox = false;
		
		JPanel radioButtos = new JPanel();
		radioButtos.setLayout(new GridLayout(3,3));
		
		/* Choices for sort settings */
		group = new ButtonGroup();

		/* Add all radio buttons to group and to the window */
		for(int i = 0; i < radioButtonsNames.length; i++)
		{
			needTextBox = false;
			radioButtons[i] = new JRadioButton();
			radioButtons[i].setText(radioButtonsNames[i]);
			radioButtos.add(radioButtons[i]);
			group.add(radioButtons[i]);
			
			/* For IP, Port and Protocol need a text box */
			for(int j = 0; j < radioButtonsWithText.length; j++)
			{
				if(radioButtonsNames[i].equals(radioButtonsWithText[j]))
				{
					needTextBox = true;
					radioButtons[i].addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent event) 
						{
							/* Show the text box */
							textFiled.setText("");
							textPanel.setVisible(true);

							/* Store the settings */
							Settings.filterParameterNeeded = true;
							Settings.filterOption = event.getActionCommand();
						}	  
					});
				}				
			}
			
			/* For other buttons doesn't need a text box */
			if(needTextBox == false)
			{
				radioButtons[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent event) 
					{
						/* Hide the text box */						
						textPanel.setVisible(false);
						
						/* Store the settings */
						Settings.filterParameterNeeded = true;
						Settings.filterOption = event.getActionCommand();
					}	  
				});
			}
		}
		
	    add(radioButtos, BorderLayout.NORTH);
	}
	
	/**
	 * This method adds standart help/finish/cancel buttons to the current frame
	 */
	private void addStandartButtons() 
	{
		Box tableBox = Box.createHorizontalBox();
		
		JButton finish = new JButton("Finish");
		finish.setToolTipText("Close and save changes");
		JButton cancel = new JButton("Cancel");
		cancel.setToolTipText("Close without saving changes");		

		tableBox.add(finish);
		tableBox.add(cancel);
		
	    add(tableBox, BorderLayout.SOUTH);
	    
		/*Finish button listener - closes the frame and stores the changes */
		finish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Settings.filterParameter = textFiled.getText();
				currentFilterFrame.dispose();
				
				Settings.filterOptionNeeded = true;
			}	  
		});
		  
		/*Cancel button listener - closes the frame*/		
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				Settings.filterParameter = textFiled.getText();				
				currentFilterFrame.dispose();		
			}	  
		});	
	}
	
	/**
	 * Function adds text box and appropriate label to the frame.
	 */
	private void addTextBox()
	{
		/* Text for inserting text */
	    JLabel label = new JLabel("Insert value for filtering:");
	    textPanel.add(label);
	    
		textFiled = new JTextField("", 15);
		textPanel.add(textFiled);		
	}
}
